package ee.homework.eighth;

import java.util.ArrayList;
import java.util.Arrays;

public class ArithmeticExpression {

	public static void main(String[] args) {
		ArithmeticExpression ae = new ArithmeticExpression();
		ArrayList<Integer> arr = new ArrayList<Integer>(Arrays.asList(2, 1, 2,
				2, 1, 1, 3, 2, 5, 2, 1, 1, 1, 2, 3, 4, 5, 2, 1));
		ArrayList<Integer> arr2 = new ArrayList<Integer>(Arrays.asList(1, 1, 1,
				1, 1, 1));
		System.out.println("number: " + ae.calculate(arr2));
	}

	private long calculate(ArrayList<Integer> numbers) {
		long calc = 1;

		int left;
		int right;
		int min;
		int index;
		for (int i = 0; i < numbers.size(); i++) {
			if (numbers.get(i) == 1) {
				if (i == 0) {
					min = numbers.get(i + 1);
					index = i + 1;
				} else if (i == numbers.size() - 1) {
					min = numbers.get(i - 1);
					index = i - 1;
				} else {
					left = numbers.get(i - 1);
					right = numbers.get(i + 1);
					if (left == right || left < right) {
						min = left;
						index = i - 1;
					} else {
						min = right;
						index = i + 1;
					}
				}
				numbers.set(index, min + 1);
				numbers.remove(i);
			}
		}

		for (Integer integer : numbers) {
			System.out.print(integer + " ");
			calc *= integer;
		}

		return calc;
	}
}
